<template>
  <div>
    <ContentWrap>
      <el-form ref="queryFormRef" :model="form" :inline="true" class="demo-form-inline mb-4">
        <el-form-item label="关联用户" prop="userId">
          <el-select
            v-model="form.userId"
            filterable
            remote
            reserve-keyword
            placeholder="请选择"
            :remote-method="remoteMethod"
            :loading="loading"
            class="!w-200px"
          >
            <el-option
              v-for="item in userOptions"
              :key="item.id"
              :label="item.nickname"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="获得时间" prop="createTime">
          <el-date-picker
            v-model="form.createTime"
            value-format="YYYY-MM-DD HH:mm:ss"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>
    <ContentWrap>
      <el-table :data="tableData" :loading="loading" stripe :show-overflow-tooltip="true">
        <el-table-column label="序列号" prop="serialNo" align="center" />
        <el-table-column label="兑换时间" prop="updateTime" align="center">
          <template #default="scope">
            {{ formatDate(scope.row.updateTime) }}
          </template>
        </el-table-column>
        <el-table-column label="兑换会员" prop="exchangeUserId" align="center" />
        <el-table-column label="关联用户" prop="userId" align="center" />
        <el-table-column label="有效期" prop="expiredTime" align="center">
          <template #default="scope">
            <span>{{
              scope.row.effectTime
                ? formatDate(scope.row.effectTime, 'YYYY-MM-DD HH:mm:ss')
                : '--'
            }}</span>
            <span> - </span>
            <span>{{
              scope.row.expiredTime
                ? formatDate(scope.row.expiredTime, 'YYYY-MM-DD HH:mm:ss')
                : '--'
            }}</span>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :total="total"
        v-model:page="form.pageNo"
        v-model:limit="form.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { getPosterReferralRelationPage, getPosterTypeList } from '@/api/allApplication/poster/index'
import { formatDate } from '@/utils/formatTime'
import { getUserPage } from '@/api/member/user'
import { PhysicalDetialApi } from '@/api/allApplication/physical/index'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
const form = ref({
  createTime:undefined,
  status: undefined,
  userId:undefined,
  pageNo: 1,
  pageSize: 10
})
const queryFormRef = ref()
const state = reactive({
  tableData: [],
  total: 0,

  posterTypeList: [] as any[]
})
const { tableData, total, posterTypeList } = toRefs(state)
const userOptions = ref([])
const loading = ref(false)
const handleSearch = () => {
  form.value.pageNo = 1
  getList()
}
const handleReset = () => {
  queryFormRef.value.resetFields()
  handleSearch()
}
const getList = async () => {
  const res = await PhysicalDetialApi.getPhysicalCardDetailPage({
    ...form.value,
    status:1,
    pageNo: form.value.pageNo,
    pageSize: form.value.pageSize
  })
  state.tableData = res.list || []
  state.total = res.total || 0
}
const getTypelist = async () => {
  const list = await getPosterTypeList()
  state.posterTypeList = list || []
}
const handleDetail = (row: any) => {
  console.log(row)
}
const remoteMethod = async (query) => {
  if (query) {
    loading.value = true
    const data = await getUserPage({ pageNo: 1, pageSize: 10, mobile: query })
    userOptions.value = data.list
    loading.value = false
  } else {
    userOptions.value = []
  }
}
onMounted(async () => {
  await getTypelist()
  await getList()
})
</script>
